package com.eagle.core.aop;

import com.eagle.core.annotation.SelfService;
import com.eagle.core.annotation.SelfTransactional;

import java.lang.reflect.Method;

/**
 * 事务代理
 */
public class SelfTransactionProxy implements SelfProxy {

    @Override
    public Object doProxy(SelfProxyChain proxyChain) throws Throwable {
        Object result;
        Method method = proxyChain.getTargetMethod();
        //加了@Transactional注解的方法要做事务处理
        if (method.isAnnotationPresent(SelfTransactional.class)) {
            try {
//                DatabaseHelper.beginTransaction();
				System.out.println("begin transaction");
                result = proxyChain.doProxyChain();
//                DatabaseHelper.commitTransaction();
				System.out.println("commit transaction");
            } catch (Exception e) {
//                DatabaseHelper.rollbackTransaction();
				System.out.println("rollback transaction");
                throw e;
            }
        } else {
            result = proxyChain.doProxyChain();
        }
        return result;
    }
}